package edu.ahpu.propertymanage.util;

import edu.ahpu.propertymanage.pojo.*;
import edu.ahpu.propertymanage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class SaveOwner {

    @Autowired
    OwnerService ownerService;

    @Autowired
    PropertyTageService propertyTageService;

    @Autowired
    GetID getID;

    @Autowired
    EleCostService eleCostService;

    @Autowired
    WaterCostService waterCostService;

    @Autowired
    GasCostServie gasCostServie;

    @Autowired
    PropCostService propCostService;

    @Autowired
    ManageCarService manageCarService;

    @Autowired
    TimeChange timeChange;


    public Boolean saveOwner(Owner owner){

        List<Owner> exitOwner = ownerService.list();

//        for (Owner o:exitOwner) {
//            if (o.getOwnerId().equals(owner.getOwnerId()))
//                return true;
//        }


        EleCost eleCost = new EleCost();
        WaterCost waterCost = new WaterCost();
        GasCost gasCost = new GasCost();
        ManageCar manageCar = new ManageCar();
        PropCost propCost = new PropCost();

        System.out.println(owner);
        PropertyTage tage = propertyTageService.getById(1);
        int size = tage.getTage().length();
        String str = getID.createOwnerUUID(owner.getOwnerArea(), owner.getOwnerDong(), owner.getOwnerUnit(), owner.getOwnerRoom());

        owner.setOwnerId(str);

        owner.setManageCarId(null);
        owner.setEleFId(null);
        owner.setWaterFId(null);
        owner.setGasFId(null);
        owner.setPropFId(null);

        owner.setOwnerPw("o" + str.substring(size + 2));
        boolean bool = ownerService.saveOrUpdate(owner);

        owner.setEleFId(str.substring(0, size + 1) + "ELE" + str.substring(size + 2));
        eleCost.setEleFId(str.substring(0, size + 1) + "ELE" + str.substring(size + 2));
        eleCost.setLastMonthEleTime(timeChange.addTime(new Date()));
        eleCost.setThisMonthEleTime(timeChange.addAMountTime(eleCost.getLastMonthEleTime()));
        eleCost.setLastMonthEleCount(0f);
        eleCost.setThisMonthEleCount(0f);

        owner.setWaterFId(str.substring(0, size + 1) + "WATER" + str.substring(size + 2));
        waterCost.setWaterFId(str.substring(0, size + 1) + "WATER" + str.substring(size + 2));
        waterCost.setLastMonthWaterTime(timeChange.addTime(new Date()));
        waterCost.setThisMonthWaterTime(timeChange.addAMountTime(waterCost.getLastMonthWaterTime()));
        waterCost.setLastMonthWaterCount(0f);
        waterCost.setThisMonthWaterCount(0f);


        owner.setGasFId(str.substring(0, size + 1) + "GAS" + str.substring(size + 2));
        gasCost.setGasFId(str.substring(0, size + 1) + "GAS" + str.substring(size + 2));
        gasCost.setLastMonthGasTime(timeChange.addTime(new Date()));
        gasCost.setThisMonthGasTime(timeChange.addAMountTime(gasCost.getLastMonthGasTime()));
        gasCost.setLastMonthGasCount(0f);
        gasCost.setThisMonthGasCount(0f);

        owner.setPropFId(str.substring(0, size + 1) + "PROP" + str.substring(size + 2));
        propCost.setPropFId(str.substring(0, size + 1) + "PROP" + str.substring(size + 2));
        propCost.setPropBeginDate(timeChange.addTime(new Date()));
        propCost.setPropEndDate(timeChange.addYearTime(propCost.getPropBeginDate()));
        propCost.setPropPrice(5);


        owner.setManageCarId(str.substring(0, size + 1) + "CAR" + str.substring(size + 2));
        manageCar.setManageCarId(str.substring(0, size + 1) + "CAR" + str.substring(size + 2));


        if (bool) {
            eleCost.setOwnerId(str);
            eleCostService.saveOrUpdate(eleCost);
            waterCost.setOwnerId(str);
            waterCostService.saveOrUpdate(waterCost);
            gasCost.setOwnerId(str);
            gasCostServie.saveOrUpdate(gasCost);
            propCost.setOwnerId(str);
            propCostService.saveOrUpdate(propCost);
            manageCar.setOwnerId(str);
            manageCarService.saveOrUpdate(manageCar);

            ownerService.saveOrUpdate(owner);
            return true;
        }

        return false;
    }
}
